

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;



public class Gprobs 
{

	
	public static void main(String[] args) 
	{
		
		 try{
			  // Open the file that is the first 
			  // command line parameter
			  FileInputStream fstream = new FileInputStream(args[0]);
			  BufferedWriter bw = new BufferedWriter(new FileWriter(args[1]));
			  // Get the object of DataInputStream
			  DataInputStream in = new DataInputStream(fstream);
			  BufferedReader br = new BufferedReader(new InputStreamReader(in));
			  String strLine,tmp;
			  //Read File Line By Line
			  List elements = null;
			  int count,idx,num_line=1,prev_count=0;
			  String substring=" -9";
			  String javapath=" | /usr/java/latest/bin/java -Xmx3000m -jar ";
			  tmp = br.readLine();
			  while ((strLine = br.readLine()) != null)   
			  {
				 
				  // Print the content on the console
				  num_line++;
				  elements=Arrays.asList(strLine.split(" "));
				  if(elements.size() %3 != 0)
				  {
					  System.out.println("some thing wrong with number of missing values Linenumber " +num_line);
					  System.exit(1);
				  }	
				  count = 0;
				  idx=0;
				  while ((idx = strLine.indexOf(substring, idx)) != -1)
				  {
					  	idx++;
				        count++;
				  }
				  if(count%3 != 0)
				  {
					  System.out.println("some thing wrong with number of missing values Linenumber " +num_line);
					  System.exit(1);
				  }
				  if(count == prev_count && num_line == 2)
				{
						bw.write(tmp);
					  bw.newLine();
				}	
				  if(count != prev_count)
				  {
					  bw.close();
					  tmp="";
					  tmp="cat "+args[1]+javapath+args[2];
					  if(num_line != 2)
					  {  
						  runGprobs(tmp);
						  System.exit(0);
					  }
					  
					  bw = new BufferedWriter(new FileWriter(args[1]));
					  tmp="marker alleleA alleleB";
					  for(int i=1;i<=elements.size()-count-3;i++)
					  {
						  tmp=tmp+" "+i+" "+i+" "+i;
						  i++;
						  i++;
					  }
					  bw.write(tmp);
					  bw.newLine();
					  tmp=strLine.replace(" -9", "");
					  bw.write(tmp);
					  bw.newLine();
					  //bw.close();
					  //System.exit(0);
				  }
				  else
				  {
					  //System.out.println(elements.size()+" "+count);
					  //.rite(elements.size()+" "+count);
					  tmp=strLine.replace(" -9", "");
					  bw.write(tmp);
					  bw.newLine();
				  }
				  prev_count=count;
			  }  
			  tmp="";
			  tmp="cat "+args[1]+javapath+args[2];
			  runGprobs(tmp);
			  	//Close the input stream
			  	in.close();
			  	bw.close();
			    File f1 = new File(args[1]);
			    f1.delete();
			}
		 	catch (Exception e)
		 	{//Catch exception if any
		 		System.err.println("Error: " + e.getMessage());
			  
			}
		 
	}
	static void runGprobs(String x)
	{
		System.out.println(x);
		
		try
		{

			Process p=Runtime.getRuntime().exec(x);
			p.waitFor();
			BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
			String line=reader.readLine();
			while(line!=null)
			{
				System.out.println(line);
				line=reader.readLine();
			}

		}
		catch(IOException e1) 
		{
			
		}
		catch(InterruptedException e2) 
		{
			
		}

		//System.out.println("finished.");
		}
}
	


